/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to HYPERFORMIX Translator
 * Author: EWALTERS
 * Created: 11:07:22 AM, Jul 30, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2hyperformix;

import java.util.Properties;

import gnu.getopt.Getopt;

/**
 * This class is the container for option information - adapted for HyPerformix
 */
public class Options {
		
	/**
	 *  Comma Separated (default) output, or AMG XML (-x)?
	 * @author EWALTERS
	 *
	 */
	public enum HyperformixOutputFormat {
		CSV_FILE {
			public String outputSuffix() {
				return "csv";
			}
		},
		XML_FILE {
			public String outputSuffix() {
				return "xml";
			}			
		};
		public abstract String outputSuffix();
	};
	
	/** Input file - must be provided */
	private String _input_fname;
	/** Output file - optional, can be constructed from input file name */
	private String _output_fname;
	/** Output format - see above enum */
	private HyperformixOutputFormat _output_format;
	/** What are the properties we are going to deal with? */
	private Properties _props;

	/**
	 * Constructor
	 */
	public Options() {
		_input_fname   = null;
		_output_fname  = null;
		_output_format = HyperformixOutputFormat.CSV_FILE;
		_props = new Properties();
	}
	
	/**
	 * Get the input file name
	 * @return the name
	 */
	public String getInputFileName() {
		return _input_fname;
	}
	
	/**
	 * Get the output file name, as derived
	 * @return the name
	 */
	public String getOutputFileName() {
		return _output_fname;
	}

	/**
	 * Which format are we using - see the above enum for details
	 * @return the format
	 */
	public HyperformixOutputFormat getOutputFormat() {
		return _output_format;
	}
	
	/**
	 * Return the Java properties	
	 * @return the props
	 */
	public Properties getProperties() {
		return _props;
	}

	/** 
	 * Get a top-level name for the input model
	 * @return
	 */
	public String getName() {
		int dot_index = _input_fname.lastIndexOf('.');
		return _input_fname.substring(0, dot_index); 		
	}

	/**
	 * Command-line argument processing using getopt()
	 * @param exec_name name of the executable (spmif2opnet)
	 * @param args command-line arguments
	 * @return true - options acceptable, false - options bad, display usage
	 */
	public boolean getOptions(String exec_name, String[] args) {
		Getopt g = new Getopt(exec_name, args, "i:o:xh");
		int inp_char;
		String arg;
		boolean response = true;
	
		while ((inp_char = g.getopt()) != -1) {
			switch(inp_char) {
			case 'i': 
				arg = g.getOptarg();
				if (arg == null) {
					response = false;
				} else {
					_input_fname = arg;
				}
				break;
			case 'o':
				arg = g.getOptarg();
				if (arg == null) {
					response = false;
				} else {
					_output_fname = arg;
				}
				break;
			case 'x':
				_output_format = HyperformixOutputFormat.XML_FILE;
				break;
			case 'h':
				response = false;
				break;
			default:
				System.out.println("Unknown option:" + inp_char);
				response = false;
			}
		}
			
		/* Set up a default for the output name if it is not there */
		if (response && (_input_fname != null) && (_output_fname == null)) {
			int dot_index = _input_fname.lastIndexOf('.');
			_output_fname = _input_fname.substring(0, dot_index) 
				+ "_hyperformix." 
				+ _output_format.outputSuffix();
		}
		
		
		/* Make sure that the input and output file names have been populated */
		if (response) {
			response = ((_input_fname != null) && (_output_fname != null)); 
		}
		return response;
	}

	/**
	 * Usage message
	 */
	public void usage() {
		System.out.println("Usage: spmif2hyperformix ");
		System.out.println("  -i <input file>");
		System.out.println(" [-o <output file>]");
		System.out.println(" [-x] (Generate XML Output File instead of CSV)");
		System.out.println(" [-h] (Print this help message)");
	}	

}
